#!/bin/bash
set -e
source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/./common.sh"

#下载依赖和工具包
function install_development() {
  yum install -y wget patch cmake 
  wget -O /etc/yum.repos.d/openEulerOS.repo https://repo.huaweicloud.com/repository/conf/openeuler_aarch64.repo
  yum clean all
  yum makecache
  yum install -y libomp
  source /etc/profile
}

#获取metaeuk
function download_metaeuk() {
  echo "# metaeuk正在下载"
  rm -rf "$1"/metaeuk
  mkdir "$1"/metaeuk
  git clone https://github.com/soedinglab/metaeuk.git  "$1"/metaeuk 
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# metaeuk已经克隆成功\033[0m"
  else
    echo -e "\033[31m# metaeuk下载失败,请重新执行脚本\033[0m"
    exit 1
  fi
}

function install_metaeuk() {
  echo -e "\033[1;32;1m# metaeuk正在安装\033[0m"
  unzip AvxToNeon-master.zip
  cp "$3"/AvxToNeon-master/*.h "$2"/bisheng-compiler-2.1.0/lib/clang/12.0.0/include
  cp "$3"/metaeuk-6-a5d39d9.patch "$1"/metaeuk
  cd "$1"/metaeuk
  git checkout -b 6-a5d39d9
  echo -e "\033[1;32;1m# 正在克隆metaeuk子模块\033[0m"
  git submodule update --init --recursive 
  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# metaeuk子模块已经克隆成功\033[0m"
  else
    echo -e "\033[31m# metaeuk子模块下载失败,请重新执行脚本\033[0m"
    exit 1
  fi

  patch -p1 <./metaeuk-6-a5d39d9.patch 
  find . -name "Makefile" -type f -exec sed -i 's/-m64/-mabi=lp64/g' {} +
  find . -name "Makefile" -type f -exec sed -i 's/-m32/-mabi=lp64/g' {} +

  find . -name "simde-features.h" -type f -exec sed -i 's/immintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/nmmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/smmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/tmmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/pmmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/emmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/xmmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/mmintrin.h/avx2neon.h/g' {} +
  find . -name "simde-features.h" -type f -exec sed -i 's/x86intrin.h/avx2neon.h/g' {} +

  find . -name "mmx.h" -type f -exec sed -i 's/mmintrin.h/avx2neon.h/g' {} +
  find . -name "xxhash.h" -type f -exec sed -i 's/immintrin.h/avx2neon.h/g' {} +
  find . -name "xxhash.h" -type f -exec sed -i 's/emmintrin.h/avx2neon.h/g' {} +
  find . -name "xxhash.h" -type f -exec sed -i 's/mmintrin.h/avx2neon.h/g' {} +
  find . -name "minigzip.c" -type f -exec sed -i 's/unix.h/avx2neon.h/g' {} +

  cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./ -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang
  make -j 4
  make install
  echo -e "\033[1;32;1m# metaeuk已经安装完毕\033[0m"
}
function cp_metaeuk() {
  mkdir -p "$2"/metaeuk
  cp -r "$1"/metaeuk/* "$2"/metaeuk
}
# 创建modulefiles文件
function write_metaeuk_module() {
  cat >"$1/metaeuk/metaeuk_modulefiles"<<EOF
#%Module1.0
conflict metaeuk
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set METAEUK \$pwd
setenv METAEUK \$METAEUK
prepend-path PATH \$METAEUK/bin
EOF
}

function main() {
  is_check_build_install_directory "$1" "$2" "metaeuk-6.sh"
  is_equal_build_install_path "$1" "$2"
  install_development
  download_metaeuk "$1"
  install_metaeuk "$1" "$2" "$3"
  cp_metaeuk "$1" "$2"
  write_metaeuk_module "$2"
  rm -rf "$1"/metaeuk-6-a5d39d9
}

main "$@"
